/**
 *@brief	�û���һЩ��ݽṹ���Լ���ɺ�ɾ����������뷽�� 
 *@date		2015-12-14
 *@author	ikangtai-wangwei
 *@copyright iknagtai corp
 **/
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include"specialCondition.h"
#include"al.h"
#include"common.h"
#include "inputBBT.h"
#include "debugTest.h"
#include "inputPeriod.h"
//#include "typeTrans.h"

#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"jni" ,__VA_ARGS__) // 定义LOGW类型

int throughtOVGetFertileAndDanger(cycleUnitDS *cycleInput,int length){
	int x = 0;
	cycleFlag* flag = initCycleFlag2();
	for(;x < length; x++){
		if(cycleInput[x].cycleData[flag->ovulationDayConfirm] > 100){
			setCalenderAfterNDays(cycleInput[x].cycleData+flag->ovulationDayConfirm,-5,cycleInput[x].cycleData+flag->fertileWindowStartConfirm);
			setCalenderAfterNDays(cycleInput[x].cycleData+flag->ovulationDayConfirm,1,cycleInput[x].cycleData+flag->fertileWindowEndConfirm);
			setCalenderAfterNDays(cycleInput[x].cycleData+flag->ovulationDayConfirm,-8,cycleInput[x].cycleData+flag->dangerWindowStartConfirm);
			setCalenderAfterNDays(cycleInput[x].cycleData+flag->ovulationDayConfirm,3,cycleInput[x].cycleData+flag->dangerWindowEndConfirm);
		}
	}
	return 1;	
}
int cleanFertileAndDanger(cycleUnitDS *cycleInput,int length){
	int x = 0;
	time_t t = 0;
	cycleFlag* flag = initCycleFlag2();
	for(;x < length; x++){
		if(cycleInput[x].cycleData[flag->ovulationDayConfirm] > 100){
			setCalenderAfterNDays(&t,0,cycleInput[x].cycleData+flag->fertileWindowStartConfirm);
			setCalenderAfterNDays(&t,0,cycleInput[x].cycleData+flag->fertileWindowEndConfirm);
			setCalenderAfterNDays(&t,0,cycleInput[x].cycleData+flag->dangerWindowStartConfirm);
			setCalenderAfterNDays(&t,0,cycleInput[x].cycleData+flag->dangerWindowEndConfirm);
		}
	}
	return 1;	
}

int theFirstUse(time_t tinput, int periodLength, int cycleLength,int mensLengthError, int cycleLengthError,
				cycleUnitDS *cycleOutput,int *cycleOutputLength,dayUnitDSOutput *daysData,int *daysDataLength,userUnitDS *userData){
	if(cycleLength < CYCLE_LEAST_LENGTH || periodLength < PERIOD_LEAST_LENGTH){
		#ifdef TEST_ALL
			LOGW("ALOUTPUT-----:something wrong:theFirstUse receive the wrong value!");
		#endif
		return ERROR_CODE;
	}
	#ifdef JAVA_OUTPUT
			LOGW("ALOUTPUT-----:theFirstUse  1!");
	#endif
	userData->validCycleCount = 0;
	userData->abnormalCaseAlert = 0;
	userData->averageCycleLength = cycleLength;
	userData->averageLuteumPhaseLength = 14;
	
	userData->averageMenstruationLength = periodLength;
	userData->averageOvulationDay = cycleLength - 14;
	userData->cycleLengthError = cycleLengthError;
	userData->menstruationLengthError = mensLengthError;
	cycleFlag* flag = initCycleFlag2();
	
	*cycleOutputLength = FIRSTUSE_START_CYCLE_INDEX+1;
	
	if(!cycleOutput || !daysData) return ERROR_CODE;
	
	cycleOutput[0].cycleNumber = 1;
	time_t tNULL = 0;

	

	
	setCalenderAfterNDays(&tinput,0,cycleOutput[0].cycleData+flag->menstruationStartConfirm);
	setCalenderAfterNDays(&tinput,periodLength - 1,cycleOutput[0].cycleData+flag->menstruationEndForecast);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->menstruationStartForecast);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->menstruationEndConfirm);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->BBTRiseDay);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->peakDay);
	setCalenderAfterNDays(&tinput,cycleLength - 14,cycleOutput[0].cycleData+flag->ovulationDayForecast);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->ovulationDayConfirm);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->ovulationDayUserRecord);
	cycleOutput[0].ovulationDayUserRecordBasis = 0;
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->ovulationDayBBTRise);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->ovulationDayNextMenstruation);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->ovulationDayForecast,0-5,
						  cycleOutput[0].cycleData+flag->fertileWindowStartForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->ovulationDayForecast,1,
						  cycleOutput[0].cycleData+flag->fertileWindowEndForecast);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->fertileWindowStartConfirm);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->fertileWindowEndConfirm);
	setCalenderAfterNDays(&tinput,periodLength+1,cycleOutput[0].cycleData+flag->dangerWindowStartForecast);
	setCalenderAfterNDays(&tinput,cycleLength - 10,cycleOutput[0].cycleData+flag->dangerWindowEndForecast);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->dangerWindowEndConfirm);
	setCalenderAfterNDays(&tNULL,0,cycleOutput[0].cycleData+flag->dangerWindowStartConfirm);
	setCalenderAfterNDays(&tinput,cycleLength,cycleOutput[0].cycleData+flag->nextMenstruationStartForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextMenstruationStartForecast,periodLength - 1,
						  cycleOutput[0].cycleData+flag->nextMenstruationEndForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextMenstruationStartForecast,cycleLength - 14, 
						  cycleOutput[0].cycleData+flag->nextOvulationDayForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextOvulationDayForecast,0-5,
						  cycleOutput[0].cycleData+flag->nextFertileWindowStartForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextOvulationDayForecast,1,
					      cycleOutput[0].cycleData+flag->nextFertileWindowEndForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextMenstruationStartForecast,periodLength+1,
					      cycleOutput[0].cycleData+flag->nextDangerWindowStartForecast);
	setCalenderAfterNDays(cycleOutput[0].cycleData+flag->nextMenstruationStartForecast,cycleLength - 10,
					      cycleOutput[0].cycleData+flag->nextDangerWindowEndForecast);
	
	// cycle 2
	int indexFlag = 0;
	for(indexFlag = 0;indexFlag < FIRSTUSE_START_CYCLE_INDEX; indexFlag++){
		throughCycleGetNextCycle(cycleOutput+indexFlag,cycleOutput+indexFlag+1,*userData);
		//throughCycleGetForeCycle(cycleOutput+FIRSTUSE_START_CYCLE_INDEX-indexFlag,cycleOutput+FIRSTUSE_START_CYCLE_INDEX-indexFlag-1,*userData);
	}
	//int startIndex[FIRSTUSE_START_CYCLE_INDEX*2+1] = {0};
	int startIndex[FIRSTUSE_START_CYCLE_INDEX+1] = {0};
	for(indexFlag = 1;indexFlag < FIRSTUSE_START_CYCLE_INDEX+1; indexFlag++){
		//����cycleOutput[0]�������Ԥ��������������е���ݾ�ΪԤ�����
		if(cycleOutput[indexFlag].cycleData[flag->menstruationStartConfirm] < 100){
			startIndex[indexFlag] = getDaysDiff(cycleOutput[0].cycleData+flag->menstruationStartConfirm,
												cycleOutput[indexFlag].cycleData+flag->menstruationStartForecast);
			
		}else{
			startIndex[indexFlag] = getDaysDiff(cycleOutput[0].cycleData+flag->menstruationStartConfirm,
												cycleOutput[indexFlag].cycleData+flag->menstruationStartConfirm);
		} 
	} 
	int addDaysBefore = 13;
	*daysDataLength = startIndex[1]+startIndex[FIRSTUSE_START_CYCLE_INDEX]+addDaysBefore;
	
	for(indexFlag = 0;indexFlag < FIRSTUSE_START_CYCLE_INDEX+1;indexFlag++){
		throughCycleGetDayData(cycleOutput+indexFlag,daysData,startIndex[indexFlag]+addDaysBefore);
	} 
	int xx = addDaysBefore;
	for(;xx > 0; xx--){
		daysData[xx-1].date = tinput-(addDaysBefore+1-xx)*24*3600;
		daysData[xx-1].dayOfCycle = 0;
		daysData[xx-1].homePageConceptionChance = 0;
		daysData[xx-1].homePageMenstruationEnd = 0;
		daysData[xx-1].homePageNextMenstruation = addDaysBefore+1-xx;
		daysData[xx-1].homePageOvulation=0;
		daysData[xx-1].periodAchieveConfirm=5;
		daysData[xx-1].periodAchieveForecast=5;
		daysData[xx-1].periodAvoidConfirm=4;
		daysData[xx-1].periodAvoidForecast=4;
	}

	

#ifdef TEST_ALL_FILE	
	printAllCycleUnit(cycleOutput[0]);
	printAllCycleUnit(cycleOutput[1]);
	printAllCycleUnit(cycleOutput[2]);
	printAllCycleUnit(cycleOutput[3]);

	int outP = 0;
	for(;outP < *daysDataLength;outP++){
		printAllDayOutputUnit(daysData[outP]);
	}
	
#endif	
	
	return RESULT_CHANGED;
}







//�������û���һ���㷨ת�Ƶ�2���㷨��ȫ�������Ҫ���¼���һ�� 
bool inputPSPEAndOV(time_t ps,time_t pe,time_t ov,cycleUnitDS *cycleOutput,int flagBit,int peLength,int cycleLength,int cycleNum){
	cycleFlag* flag = initCycleFlag2();
	cycleOutput->cycleNumber = cycleNum;
	setCalenderAfterNDays(&ps,0,cycleOutput->cycleData+flag->menstruationStartForecast);
	if(flagBit & 0x01){
		setCalenderAfterNDays(&ps,0,cycleOutput->cycleData+flag->menstruationStartConfirm);
	}
	setCalenderAfterNDays(&ps,peLength,cycleOutput->cycleData+flag->menstruationEndForecast);
	if(flagBit & 0x02){
		setCalenderAfterNDays(&pe,0,cycleOutput->cycleData+flag->menstruationEndConfirm);
	}
	setCalenderAfterNDays(&ov,0,cycleOutput->cycleData+flag->ovulationDayForecast);
	
	if(flagBit & 0x04){
		//��ʾ��BBT
		setCalenderAfterNDays(&ov,0,cycleOutput->cycleData+flag->BBTRiseDay);
	
		setCalenderAfterNDays(&ov,0,cycleOutput->cycleData+flag->ovulationDayConfirm);	
		setCalenderAfterNDays(&ov,0,cycleOutput->cycleData+flag->ovulationDayBBTRise); 
		setCalenderAfterNDays(&ov,0-5,cycleOutput->cycleData+flag->fertileWindowStartConfirm);
		setCalenderAfterNDays(&ov,1,cycleOutput->cycleData+flag->fertileWindowEndConfirm);
		setCalenderAfterNDays(&ov,0-6,cycleOutput->cycleData+flag->dangerWindowStartConfirm);
		setCalenderAfterNDays(&ov,3,cycleOutput->cycleData+flag->dangerWindowEndConfirm);
	}
	if(flagBit & 0x08){
		setCalenderAfterNDays(&ov,0,cycleOutput->cycleData+flag->ovulationDayNextMenstruation);	
	}
	
	
	
	
	
	
	setCalenderAfterNDays(&ov,0-5,cycleOutput->cycleData+flag->fertileWindowStartForecast);
	setCalenderAfterNDays(&ov,1,cycleOutput->cycleData+flag->fertileWindowEndForecast);
	
	setCalenderAfterNDays(&ov,0-8,cycleOutput->cycleData+flag->dangerWindowStartForecast);
	setCalenderAfterNDays(&ov,3,cycleOutput->cycleData+flag->dangerWindowEndForecast);

	setCalenderAfterNDays(&ps,cycleLength,cycleOutput->cycleData+flag->nextMenstruationStartForecast);
	setCalenderAfterNDays(&ps,cycleLength+peLength,cycleOutput->cycleData+flag->nextMenstruationEndForecast);
	setCalenderAfterNDays(&ov,cycleLength,cycleOutput->cycleData+flag->nextOvulationDayForecast);
	setCalenderAfterNDays(cycleOutput->cycleData+flag->nextOvulationDayForecast,0-5,cycleOutput->cycleData+flag->nextFertileWindowStartForecast);
	setCalenderAfterNDays(cycleOutput->cycleData+flag->nextOvulationDayForecast,1,cycleOutput->cycleData+flag->nextFertileWindowEndForecast);
	setCalenderAfterNDays(cycleOutput->cycleData+flag->nextOvulationDayForecast,0-6,cycleOutput->cycleData+flag->nextDangerWindowStartForecast);
	setCalenderAfterNDays(cycleOutput->cycleData+flag->nextOvulationDayForecast,3,cycleOutput->cycleData+flag->nextDangerWindowEndForecast);
	return true;

}
/*
�����ڸմ����ǵ�һ��APPת�Ƶ��ڶ���APP���û������ǵ������Ҫȫ�����¼��� 
the dayoutput is just the last ps before and after 150days   
*/
int transV1TOV2DS(int length,dayUnitDSInput *daysInput,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT-----:transV1TOV2DS start!\n");
	#endif
	if(!daysInput || !daysOutput || !cycleOutput || !userData) return ERROR_CODE;
	int inputIndex = 0;
	int sumLengthPeriod = 0;
	int sumLengthCycle  = 0;
	int sumLengthLuPh   = 0;
	int psCount = 0;
	int peCount = 0;
	int peCountValid = 0;
	int luCount = 0;
	int lastCycleLength = 0;
	int lastPerLength  = 0;
	int sumLengthPeriodErr = 0;
	int perErrCount = 0;
	int sumLengthCycleErr = 0;
	int cycleErrCount = 0;
	//we just consider the last 12 cycles 
	int psIndex[12] = {0}; 
	int peIndex[12] = {0};
	time_t lastPS = 0;
	time_t lastPE = 0;
	int sumPs = 0; 
	//1���ҵ�ÿ�����ڿ�ʼ�ͽ���,����ÿ�����ڵ�λ�ô洢����Ӧ��������psIndex��peIndex 
	for(inputIndex = 0;inputIndex < length; inputIndex++){
		// find the sum of ps
		if(daysInput[inputIndex].menstruationRecord == 1){
			sumPs++;
		} 
	}
	
	sumPs=sumPs>12?12:sumPs;
	int cycleOutputIndex = 0; 
	for(inputIndex = length-1;inputIndex >= 0; inputIndex--){
		if(daysInput[inputIndex].menstruationRecord == 1){
			//%20��Ŀ����Ҫ��Ϊ�˷�ֹ����Խ�� 
			if(psCount < 120){
				psIndex[psCount%120] = inputIndex;
				psCount++;	
			}
			
		}
		if(daysInput[inputIndex].menstruationRecord == 2){
			if(peCount < 120){
				peIndex[peCount%120] = inputIndex;
				peCount++;	
			}
		
		}		
	}
	int cycleNum = psCount > peCount? psCount+CYCLE_OUTPUT_LENGTH:peCount+CYCLE_OUTPUT_LENGTH;
	//�����ɳ�������ɣ��������16������ 
	//cycleUnitDS * cycleResult = createCycleUnitNum(cycleNum);
	//createCycleNum = cycleNum;
	//if(cycleResult == NULL) return ERROR_CODE;
	
	//2,����ÿ�����ڵ�������,���ҷֱ����ÿ�����ڵ�cycleData 
	int psStart = 0;
	for(;psStart < psCount-1;psStart++){
		int cycleLength = getDaysDiff(&(daysInput[psIndex[psStart]].date),&(daysInput[psIndex[psStart+1]].date)); 
		sumLengthCycle += cycleLength;
		if(cycleErrCount == 0){
			lastCycleLength = cycleLength;
		}else{
			int thisTimeCycleLengthErr = lastCycleLength - cycleLength;
			thisTimeCycleLengthErr = thisTimeCycleLengthErr>0?thisTimeCycleLengthErr:0-thisTimeCycleLengthErr;
			sumLengthCycleErr+=thisTimeCycleLengthErr;
			cycleErrCount++;
		}
		int start = 0;
		int validCount = 0;
		bool ovConfirm = false; 
		for(start = psIndex[psStart];start < psIndex[psStart+1]; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
			if(daysInput[start].tempFlag == 0){
				validCount++;
			}
		}
		//��ȡ��PE
		int peStart = 0;
		for(;peStart<peCount;peStart++){
			if(peIndex[peStart] > psIndex[psStart] && peIndex[peStart] < psIndex[psStart+1]){
				break;
			}
		}
		time_t pe = NULL;
		int flagBit = 0;
		int perLengthNow = 0;
		if(peStart<peCount){
			int perLength = getDaysDiff(&(daysInput[psIndex[psStart]].date),&(daysInput[peIndex[peStart]].date));
			if(perLength > 10){
				setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageMenstruationLength-1,&pe);
				perLengthNow = 4;
			}else{
				sumLengthPeriod+= perLength;
				peCountValid++;
				flagBit |= 0x02;
				pe = daysInput[peIndex[peStart]].date;
				perLengthNow = perLength;
			}
		}else{
			//�����û������PE
			setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageMenstruationLength-1,&pe);
			perLengthNow = 4;
		}
		bool ovCreate = false;
		time_t ov = 0;
		//��ȡ��OV 
		if(validCount > 10){
			BBTUnitDS *BBTArray = (BBTUnitDS *)malloc(sizeof(BBTUnitDS)*validCount);
			if(BBTArray == NULL) return false;
			for(start = psIndex[psStart];start < psIndex[psStart+1]; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
				int BBTArrayIndex = 0;
				if(daysInput[start].tempFlag == 0){
					BBTArray[BBTArrayIndex].index = start;
					BBTArray[BBTArrayIndex].BBT   = daysInput[start].BBT;

				}
			}
			int ovNext = 0;
			int ovIndex = BBTTestOvulationDay(BBTArray,validCount,&ovNext);
			
			
			if(ovIndex != -1){
				ovConfirm = true; 
				//˵�����μ�����������
				int luLength = getDaysDiff(&(daysInput[ovIndex].date),&(daysInput[psIndex[psStart+1]].date));
				if(luLength < 18){
					sumLengthLuPh+= luLength;
					luCount++;
				}
				ov = daysInput[ovIndex].date;
				flagBit |= 0x05;

				inputPSPEAndOV(daysInput[psIndex[psStart]].date,pe,ov,cycleOutput+cycleOutputIndex+psStart,flagBit,perLengthNow,cycleLength,psStart);
				
			} 
			 
		}
		if(ovConfirm == false){
			//˵������������TODO:��ӱ�Cycleû�м�������� ��Ҳ����˵ֻ��һ��PS�����õģ�����������PS�ĺ��� 
			setCalenderAfterNDays(&(daysInput[psIndex[psStart+1]].date),0-14,&ov);
			flagBit |= 0x08;
			inputPSPEAndOV(daysInput[psIndex[psStart]].date,pe,ov,cycleOutput+cycleOutputIndex+psStart,flagBit,perLengthNow,cycleLength,psStart);	
		}
	}
	userData->validCycleCount = psCount;
	if(psCount == 0){
		userData->averageCycleLength = 28;
	}else{
		userData->averageCycleLength = sumLengthCycle/psCount;
	}
	if(luCount == 0){
		userData->averageLuteumPhaseLength = 14;
	}else{
		userData->averageLuteumPhaseLength = sumLengthLuPh/luCount;
	}
	
	if(peCountValid == 0){
		userData->averageMenstruationLength = 5;
	}else{
		userData->averageMenstruationLength = sumLengthPeriod/peCountValid;
	}
	if(cycleErrCount == 0){
		userData->cycleLengthError = 4;
	}else{
		userData->cycleLengthError = sumLengthCycleErr/cycleErrCount;
	}
	userData->averageOvulationDay = userData->averageCycleLength - userData->averageMenstruationLength;
	userData->menstruationLengthError = 2;
	userData->abnormalCaseAlert = 0;
	//2.1���㱾���ڵ����
	int start;
	int flagBits = 0x01;
	int validCount = 0; 
	for(start = psIndex[psStart];start < length; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
		if(daysInput[start].tempFlag == 0 && daysInput[start].BBT < 42 && daysInput[start].BBT > 32){
			validCount++;
		}
	}
	int peStart = 0;
	for(;peStart<peCount;peStart++){
		if(peIndex[peStart] > psIndex[psStart]){
			break;
		}
	}
	time_t pe = NULL;
	int perLengthNow = 0;
	int flagBit = 0; 
	if(peStart<peCount){
		int perLength = getDaysDiff(&(daysInput[psIndex[psCount-1]].date),&(daysInput[peIndex[peStart]].date));
		if(perLength > 10){
			setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageMenstruationLength-1,&pe);
			perLengthNow = userData->averageMenstruationLength-1;
		}else{
			flagBit|= 0x02;
			pe = daysInput[peIndex[peStart]].date;
			perLengthNow = perLength;
		}
	}else{
		//�����û������PE
		setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageMenstruationLength-1,&pe);
		perLengthNow = userData->averageMenstruationLength-1;
	}
	time_t ov = 0;		
	
	if(validCount < 10){
		//��OVday
		setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageOvulationDay,&ov);
		inputPSPEAndOV(daysInput[psIndex[psCount-1]].date,pe,ov,cycleOutput+cycleOutputIndex+psCount-1,flagBits,perLengthNow,userData->averageCycleLength,psCount-1);	
		
	}else{
		BBTUnitDS *BBTArray = (BBTUnitDS *)malloc(sizeof(BBTUnitDS)*validCount);
		int initX = 0;
		for(;initX < validCount; validCount++){
			BBTArray[initX].BBT = 0;
			BBTArray[initX].index = 0;
		} 
		for(start = psIndex[psStart];start < psIndex[psStart+1]; start++){
		//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
			int BBTArrayIndex = 0;
			if(daysInput[start].tempFlag == 0 && daysInput[start].BBT < 42 && daysInput[start].BBT > 32){
				BBTArray[BBTArrayIndex].index = start;
				BBTArray[BBTArrayIndex].BBT   = daysInput[start].BBT;
			}
		}
		int ovNext = 0;
		int ovIndex = BBTTestOvulationDay(BBTArray,validCount,&ovNext);
		if(ovIndex != -1){
				
				//˵�����μ�����������
			flagBit |= 0x05;
			ov = daysInput[ovIndex].date;
			inputPSPEAndOV(daysInput[psIndex[psCount-1]].date,pe,ov,cycleOutput+cycleOutputIndex+psCount-1,flagBits,perLengthNow,userData->averageCycleLength,psCount-1);
			
		}else{
			setCalenderAfterNDays(&(daysInput[psIndex[psCount-1]].date),userData->averageOvulationDay,&ov);
			inputPSPEAndOV(daysInput[psIndex[psCount-1]].date,pe,ov,cycleOutput+cycleOutputIndex+psCount-1,flagBits,perLengthNow,userData->averageCycleLength,psCount-1);	
		}
		
	}
	 
	//3,���ÿ�����ڵ�cycleData����ÿ�����ڵ�dayData
	int psStartN = 0;
	for(;psStartN < psCount;psStartN++){
		if(psStartN == 0){
			throughCycleGetDayData(cycleOutput+psStartN,daysOutput,0);
		}else{
			int length = getDaysDiff(&(daysInput[psIndex[0]].date),&(daysInput[psIndex[psStartN]].date));
			throughCycleGetDayData(cycleOutput+psStartN,daysOutput,length);	
		}
		
	}
	int lengthN = getDaysDiff(&(daysInput[psIndex[0]].date),&(daysInput[psIndex[psCount-1]].date));
	
	throughCycleGetNextCycle(cycleOutput+psCount-1,cycleOutput+psCount,*userData);
	throughCycleGetNextCycle(cycleOutput+psCount,cycleOutput+psCount+1,*userData);
	
	throughCycleGetDayData(cycleOutput+psCount,daysOutput,lengthN+userData->averageCycleLength);
	throughCycleGetDayData(cycleOutput+psCount+1,daysOutput,lengthN+(userData->averageCycleLength)*2);
//	getAllOutput(cycleOutput,psCount+1,daysOutput,userData,&());
	*dayOutLength = lengthN+(userData->averageCycleLength)*3;
	*cycleOutLength =  psCount+3;
	int printIndex = 0;
	for(;printIndex < *cycleOutLength; printIndex++){
		printAllCycleUnit(cycleOutput[printIndex]);
	}
	for(printIndex = 0;printIndex < *dayOutLength; printIndex++){
		printAllDayOutputUnit(daysOutput[printIndex]);
	}
	
	return 1;
	
	
}



int transV1TOV2DS_V2(int length,dayUnitDSInput *daysInput,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT-----:transV1TOV2DS start!\n");
	#endif
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start \n ");
	#endif
	if(!daysInput || !daysOutput || !cycleOutput || !userData) return ERROR_CODE;
	int delayCount = 10;
	int inputIndex = 0;
	int sumLengthPeriod = 0;
	int sumLengthCycle  = 0;
	int sumLengthLuPh   = 0;
	int psCount = 0;
	int peCount = 0;
	int peCountValid = 0;
	int luCount = 0;
	int lastCycleLength = 0;
	int lastPerLength  = 0;
	int sumLengthPeriodErr = 0;
	int perErrCount = 0;
	int sumLengthCycleErr = 0;
	int cycleErrCount = 0;
	//we just consider the last 12 cycles 
	int psIndex[100] = {0};
	int peIndex[100] = {0};
	time_t lastPS = 0;
	time_t lastPE = 0;
	int sumPs = 0; 
	//1���ҵ�ÿ�����ڿ�ʼ�ͽ���,����ÿ�����ڵ�λ�ô洢����Ӧ��������psIndex��peIndex 
	for(inputIndex = 0;inputIndex < length; inputIndex++){
		// find the sum of ps
		if(daysInput[inputIndex].menstruationRecord == 1){
			sumPs++;
		} 
	}
	
	//sumPs=sumPs>12?12:sumPs;
	int cycleOutputIndex = 0;
	/*
	if(sumPs < 4){
		cycleOutputIndex = 2;
	}
	*/
	int sumIndex = sumPs-1; 
	for(inputIndex = length-1;inputIndex >= 0; inputIndex--){
		if(daysInput[inputIndex].menstruationRecord == 1){
			psIndex[sumIndex] = inputIndex;
			sumIndex--;
		}
	}
	sumIndex = sumPs - 1;
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 2 \n ");
	#endif
	for(inputIndex = length-1;inputIndex >= 0; inputIndex--){	
		if(daysInput[inputIndex].menstruationRecord == 2){
			int in = 0;
			for(in = sumIndex; in >= 0 ; in--){
				int diff = getDaysDiff(&(daysInput[psIndex[in]].date),&(daysInput[inputIndex].date));
				if(diff < 10 && diff >= 0){
					peIndex[in] = inputIndex;
					break; 
				}
				
			}
			if(in == -1){
				break;
			}
		
		
		}
	
	}
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 3\n ");
	#endif
	int cycleNum = sumPs+CYCLE_OUTPUT_LENGTH;
	//�����ɳ�������ɣ��������16������ 
	cycleUnitDS * cycleResult = createCycleUnitNum(cycleNum);
	createCycleNum = cycleNum;
	cycleFlag* flag = initCycleFlag2();
	if(cycleResult == NULL) return ERROR_CODE;
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 4\n ");
	#endif
	//2,����ÿ�����ڵ�������,���ҷֱ����ÿ�����ڵ�cycleData 
	int psStart = 0;
	int psPeSum = 0;
	for(;psStart < sumPs-1;psStart++){
		int flagBit = 0x01;
		time_t ps = NULL;
		setCalenderAfterNDays(&(daysInput[psIndex[psStart]].date),0,&ps);
	//	setCalenderAfterNDays(&(daysInput[psIndex[psStart]].date),0,cycleOutput[psStart].cycleData+flag->menstruationStartConfirm);
	//	setCalenderAfterNDays(&(daysInput[psIndex[psStart]].date),0,cycleOutput[psStart].cycleData+flag->menstruationStartForecast);
		
		int cycleLength = getDaysDiff(&(daysInput[psIndex[psStart]].date),&(daysInput[psIndex[psStart+1]].date)); 
		
		
		sumLengthCycle += cycleLength;
		if(cycleErrCount == 0){
			lastCycleLength = cycleLength;
		}else{
			int thisTimeCycleLengthErr = lastCycleLength - cycleLength;
			thisTimeCycleLengthErr = thisTimeCycleLengthErr>0?thisTimeCycleLengthErr:0-thisTimeCycleLengthErr;
			sumLengthCycleErr+=thisTimeCycleLengthErr;
			cycleErrCount++;
		}
		int start = 0;
		int validCount = 0;
		bool ovConfirm = false; 
		for(start = psIndex[psStart];start < psIndex[psStart+1] && start < length; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
			if(daysInput[start].tempFlag == 0 && daysInput[start].BBT < 42 && daysInput[start].BBT > 32){
				validCount++;
			}
		}
		//��ȡ��PE
		time_t pe = NULL;
		
		int sumPeriodLength = 0;
		if(peIndex[psStart] != 0){
			//this period end is exist
			int perLength = getDaysDiff(&(daysInput[psIndex[psStart]].date),&(daysInput[peIndex[psStart]].date));
			if(perLength > 0 && perLength < 10){
				psPeSum++;
				sumLengthPeriod+= perLength;
				flagBit|= 0x02;
				pe = daysInput[peIndex[psStart]].date;
				//setCalenderAfterNDays(&(daysInput[peIndex[psStart]].date),0,cycleOutput[psStart].cycleData+flag->menstruationEndConfirm);
				//setCalenderAfterNDays(&(daysInput[psIndex[psStart]].date),0,cycleOutput[psStart].cycleData+flag->menstruationEndForecast);
			}else{
				setCalenderAfterNDays(&(daysInput[psIndex[psStart]].date),0,&pe);
			}
		}
		bool ovCreate = false;
		time_t ov = 0;
		//��ȡ��OV 
		if(validCount > 10){
			BBTUnitDS *BBTArray = (BBTUnitDS *)malloc(sizeof(BBTUnitDS)*validCount);
			if(BBTArray == NULL) return false;
			int BBTArrayIndex = 0;
			int initX = 0;
			for(; initX < validCount; initX++){
				BBTArray[initX].BBT = 0;
				BBTArray[initX].index = 0;
			}
			for(start = psIndex[psStart];start < psIndex[psStart+1]; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
				
				if(daysInput[start].tempFlag == 0 && daysInput[start].BBT > 32 && daysInput[start].BBT < 42){
					BBTArray[BBTArrayIndex].index = start;
					BBTArray[BBTArrayIndex].BBT   = daysInput[start].BBT;
					BBTArrayIndex++;
				}
			}
			int ovNext = 0;
			int ovIndex = BBTTestOvulationDay(BBTArray,validCount,&ovNext);
			
			
			if(ovIndex != -1){
				//ovConfirm = true; 
				//˵�����μ�����������
				int luLength = getDaysDiff(&(daysInput[ovIndex].date),&(daysInput[psIndex[psStart+1]].date));
				if(luLength < 16 && luLength > 12){
					sumLengthLuPh += luLength;
					luCount++;
				}
				ov = daysInput[ovIndex].date;
				flagBit |= 0x04;
				ovConfirm = true;
				inputPSPEAndOV(daysInput[psIndex[psStart]].date,pe,ov,cycleOutput+cycleOutputIndex+psStart,flagBit,4,cycleLength,psStart+1);
				
			}
			 
		}
		if(ovConfirm == false){
			//˵������������TODO:��ӱ�Cycleû�м�������� ��Ҳ����˵ֻ��һ��PS�����õģ�����������PS�ĺ��� 
			setCalenderAfterNDays(&(daysInput[psIndex[psStart+1]].date),0-14,&ov);
			flagBit |= 0x08;
			inputPSPEAndOV(daysInput[psIndex[psStart]].date,pe,ov,cycleOutput+cycleOutputIndex+psStart,flagBit,4,cycleLength,psStart+1);	
		}
	}
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 5\n ");
	#endif
	userData->validCycleCount = sumPs - 1;
	if(sumPs <= 1){
		if(userData->averageCycleLength < 20) userData->averageCycleLength = 28;
	}else{
		userData->averageCycleLength = sumLengthCycle/(sumPs-1);
		if(userData->averageCycleLength < 20) userData->averageCycleLength = 28;
	}
	if(luCount == 0){
		userData->averageLuteumPhaseLength = 14;
	}else{
		userData->averageLuteumPhaseLength = sumLengthLuPh/luCount;
		if(userData->averageLuteumPhaseLength > 16 || userData->averageLuteumPhaseLength < 12){
			userData->averageLuteumPhaseLength = 14;
		}
	}
	
	if(psPeSum == 0){
		if(userData->averageMenstruationLength <= 2) userData->averageMenstruationLength = 5;
	}else{
		userData->averageMenstruationLength = sumLengthPeriod/psPeSum;
		if(userData->averageMenstruationLength <= 2) 
		{
			userData->averageMenstruationLength = 5;
		}
	}
	if(cycleErrCount == 0){
		userData->cycleLengthError = 4;
	}else{
		userData->cycleLengthError = sumLengthCycleErr/cycleErrCount;
	}
	userData->averageOvulationDay = userData->averageCycleLength - userData->averageLuteumPhaseLength;
	userData->menstruationLengthError = 2;
	userData->abnormalCaseAlert = 0;
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 6\n ");
	#endif
	//2.1���㱾���ڵ����
	int start;
	int flagBit = 0x01;

	int peStart = 0;
	time_t pe = NULL;
	int perLengthNow = 0;
	if(peIndex[sumPs-1] != 0){
		int perLength = getDaysDiff(&(daysInput[psIndex[sumPs-1]].date),&(daysInput[peIndex[sumPs-1]].date));
		if(perLength > 0 && perLength < 10){
			flagBit|= 0x02;
			pe = daysInput[peIndex[sumPs-1]].date;
			perLengthNow = perLength;
		}else{
			setCalenderAfterNDays(&(daysInput[psIndex[sumPs-1]].date),userData->averageMenstruationLength-1,&pe);
			perLengthNow = userData->averageMenstruationLength-1;
		} 
	}else{
		setCalenderAfterNDays(&(daysInput[psIndex[sumPs-1]].date),userData->averageMenstruationLength-1,&pe);
		perLengthNow = userData->averageMenstruationLength-1;
	}
	
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 7\n ");
	#endif
	time_t ov = 0;		
	int validCount = 0; 
	for(start = psIndex[sumPs-1];start < length; start++){
			//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
		if(daysInput[start].tempFlag == 0 && daysInput[start].BBT > 32 && daysInput[start].BBT < 42){
			validCount++;
		}
	}
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 8\n ");
	#endif
	if(validCount < 10){
		//��OVday
		setCalenderAfterNDays(&(daysInput[psIndex[sumPs-1]].date),userData->averageOvulationDay,&ov);
		inputPSPEAndOV(daysInput[psIndex[sumPs-1]].date,pe,ov,cycleOutput+cycleOutputIndex+sumPs-1,flagBit,perLengthNow,userData->averageCycleLength,sumPs);	
		
	}else{
		BBTUnitDS *BBTArray = (BBTUnitDS *)malloc(sizeof(BBTUnitDS)*validCount); 
		if(!BBTArray) return ERROR_CODE;
		int BBTArrayIndex = 0;
		int initX = 0;
		for(; initX < validCount; initX++){
			BBTArray[initX].BBT = 0;
			BBTArray[initX].index = 0;
		}
		
		for(start = psIndex[psStart];start < length; start++){
		//int BBTTestOvulationDay(BBTUnitDS BBTArray[],int length){
			
			if(daysInput[start].tempFlag == 0 && daysInput[start].BBT > 32){
				BBTArray[BBTArrayIndex].index = start;
				BBTArray[BBTArrayIndex].BBT   = daysInput[start].BBT;
				BBTArrayIndex++;
			}
		}
		int ovNext = 0;
		int ovIndex = BBTTestOvulationDay(BBTArray,validCount,&ovNext);
		if(ovIndex != -1){
				
				//˵�����μ�����������
			flagBit |= 0x04;
			ov = daysInput[ovIndex].date;
			inputPSPEAndOV(daysInput[psIndex[sumPs-1]].date,pe,ov,cycleOutput+cycleOutputIndex+sumPs-1,flagBit,perLengthNow,userData->averageCycleLength,sumPs);
			
		}else{
			setCalenderAfterNDays(&(daysInput[psIndex[sumPs-1]].date),userData->averageOvulationDay,&ov);
			inputPSPEAndOV(daysInput[psIndex[sumPs-1]].date,pe,ov,cycleOutput+cycleOutputIndex+sumPs-1,flagBit,perLengthNow,userData->averageCycleLength,sumPs);	
		}
		
	}
	 #ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 9\n ");
	#endif
	//3,���ÿ�����ڵ�cycleData����ÿ�����ڵ�dayData
	int psStartN = 0;
	int dayOutStart = 10; 
/* 
	if(sumPs < 4){
		//�������ľ����������٣�����4���Ļ�������֮ǰ����������
		throughCycleGetForeCycle(cycleOutput+cycleOutputIndex,cycleOutput+cycleOutputIndex-1,*userData);
		throughCycleGetForeCycle(cycleOutput+cycleOutputIndex-1,cycleOutput+cycleOutputIndex-2,*userData);
		throughCycleGetDayData(cycleOutput+cycleOutputIndex-2,daysOutput,0);
		throughCycleGetDayData(cycleOutput+cycleOutputIndex-1,daysOutput,userData->averageCycleLength);
		dayOutStart = 2*userData->averageCycleLength;
	}
*/	
#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 10\n ");
	#endif
	/*
	for(;psStartN < sumPs;psStartN++){
		if(psStartN == 0){
			throughCycleGetDayData(cycleOutput+cycleOutputIndex+psStartN,daysOutput,dayOutStart);
		}else{
			int length = getDaysDiff(&(daysInput[psIndex[0]].date),&(daysInput[psIndex[psStartN]].date))+dayOutStart;
			throughCycleGetDayData(cycleOutput+cycleOutputIndex+psStartN,daysOutput,length);	
		}
		
	}*/
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 11\n ");
	#endif
	int lengthN = getDaysDiff(&(daysInput[psIndex[0]].date),&(daysInput[psIndex[sumPs-1]].date));
	
	throughCycleGetNextCycle(cycleOutput+cycleOutputIndex+sumPs-1,cycleOutput+cycleOutputIndex+sumPs,*userData);
	throughCycleGetNextCycle(cycleOutput+cycleOutputIndex+sumPs,cycleOutput+cycleOutputIndex+sumPs+1,*userData);
	throughCycleGetNextCycle(cycleOutput+cycleOutputIndex+sumPs+1,cycleOutput+cycleOutputIndex+sumPs+2,*userData);	
//	throughCycleGetDayData(cycleOutput+cycleOutputIndex+sumPs,daysOutput,dayOutStart+lengthN+userData->averageCycleLength);
//	throughCycleGetDayData(cycleOutput+cycleOutputIndex+sumPs+1,daysOutput,dayOutStart+lengthN+(userData->averageCycleLength)*2);
//	throughCycleGetDayData(cycleOutput+cycleOutputIndex+sumPs+2,daysOutput,dayOutStart+lengthN+(userData->averageCycleLength)*3);
//	getAllOutput(cycleOutput,psCount+1,daysOutput,userData,&());
	*dayOutLength = lengthN+(userData->averageCycleLength)*3+dayOutStart;
	*cycleOutLength =  sumPs+3+cycleOutputIndex;
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 12\n ");
	#endif
/*	int xx = dayOutStart;
	for(;xx > 0; xx--){
		daysOutput[xx-1].date = daysOutput[dayOutStart].date-(delayCount+1-xx)*24*3600;
		daysOutput[xx-1].dayOfCycle = 0;
		daysOutput[xx-1].homePageConceptionChance = 0;
		daysOutput[xx-1].homePageMenstruationEnd = 0;
		daysOutput[xx-1].homePageNextMenstruation = dayOutStart+1-xx;
		daysOutput[xx-1].homePageOvulation=0;
		daysOutput[xx-1].periodAchieveConfirm=5;
		daysOutput[xx-1].periodAchieveForecast=5;
		daysOutput[xx-1].periodAvoidConfirm=4;
		daysOutput[xx-1].periodAvoidForecast=4;
	}
*/	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2 start 13\n ");
	#endif
	
	
#ifdef TEST_ALL_FILE
	int printIndex = 0;
	for(;printIndex < *cycleOutLength; printIndex++){
		printAllCycleUnit(cycleOutput[printIndex]);
	}
	for(printIndex = 0;printIndex < *dayOutLength; printIndex++){
		printAllDayOutputUnit(daysOutput[printIndex]);
	}
#endif	
	return 1;
	
	
}
//�����û���ݴ��ҵ�ʱ�򣬿�������������� 
int transV2TOV2DS(int length,dayUnitDSInput *daysInput,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	int x = 0;
	int y = 0;
	int mensIndex[100] = {0};
	time_t mensDate[100] = {0};
	int mensLength = 0;
	int index = 0;
	for(;x < length; x++){
		if(daysInput[x].menstruationRecord > 0){
			daysInput[x].menstruationRecord = 1;
			mensIndex[index] = x;
			mensDate[index] = daysInput[x].date;
			index++;
		}
	}
	mensLength = index;
	for(x = 0;x < mensLength ; x++){
		
		for(y=x+1; y < mensLength; y++ ){
			int diff = getDaysDiff(&(mensDate[x]),&(mensDate[y]));
			if(diff > 9){
				int z = 0;
				for(z = mensIndex[x]+1; z < mensIndex[y]; z++){
					daysInput[z].menstruationRecord = 0;
				}
				int diff2 = getDaysDiff(&(mensDate[x]),&(daysInput[mensIndex[y-1]].date));
				if(diff2 >= 2){
					daysInput[mensIndex[y-1]].menstruationRecord = 2;
				}else if(diff2 > 0){
					daysInput[mensIndex[y-1]].menstruationRecord = 0;
				}
				x = y-1;
				break;	
			}
			
		}
		
	} 
//	for(int x = 0; x < length; x++){
//		LOGW("index:%d    ments:%d",x,daysInput[x].menstruationRecord);
//	}
	return transV1TOV2DS_V2(length,daysInput,daysOutput,cycleOutput,userData,dayOutLength,cycleOutLength);
}
//�û��������ݿ��ܻ������⣬��Ҫ��ǰ���й��� 
int transV1TOV2DS_V2_Filer(int length,dayUnitDSInput *daysInput,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2_Filer start \n ");
	#endif
	int x = 0;
	int y = 0;
	int mensIndex[200] = {0};
	time_t mensDate[200] = {0};
	int mensLength = 0;
	int index = 0;
	for(;x < length; x++){
		if(daysInput[x].menstruationRecord > 0){
			daysInput[x].menstruationRecord = 1;
			mensIndex[index] = x;
			mensDate[index] = daysInput[x].date;
			index++;
		}
	}
	mensLength = index;
	//��һ�ֹ��ˣ����˳����еľ��ڿ�ʼ�;��ڽ��� 
	for(x = 0;x < mensLength ; x++){
		for(y=x+1; y < mensLength; y++ ){
			int diff = getDaysDiff(&(mensDate[x]),&(mensDate[y]));
			if(diff > MOST_PERIOD_LENGTH && diff < 150){
				int z = 0;
				for(z = mensIndex[x]+1; z < mensIndex[y]; z++){
					daysInput[z].menstruationRecord = 0;
				}
				int diff2 = getDaysDiff(&(mensDate[x]),&(daysInput[mensIndex[y-1]].date));
				if(diff2 >= 2){
					daysInput[mensIndex[y-1]].menstruationRecord = 2;
				}else if(diff2 > 0){
					daysInput[mensIndex[y-1]].menstruationRecord = 0;
				}
				x = y-1;
				break;	
			}else if(diff >= 150 ){
				
				
			}
		}
		if(y == mensLength){
			int diff = getDaysDiff(&(mensDate[x]),&(mensDate[y-1]));
			if(diff < MOST_PERIOD_LENGTH){
				int z = 0;
				for(z = mensIndex[x]+1; z < mensIndex[y-1]; z++){
					daysInput[z].menstruationRecord = 0;
				}
				int diff2 = getDaysDiff(&(mensDate[x]),&(daysInput[mensIndex[y-1]].date));
				if(diff2 >= 2){
					daysInput[mensIndex[y-1]].menstruationRecord = 2;
				}else if(diff2 > 0){
					daysInput[mensIndex[y-1]].menstruationRecord = 0;
				}
				
			}
		}
		
	} 
 	
	for(x = 0; x < length; x++){
		LOGW("\nindex:%d    ments:%d\n",x,daysInput[x].menstruationRecord);
	}
	//���ֹ��ˣ����˵Ĺ���ǣ��Ӻ���ǰ�ң�����һ�����������׼�ģ���ǰ��ѯ�������жϾ��룬��������n����n-1���ڵ�
	//��������ֱ�ӽضϣ�ǰ��Ĳ��ٽ��м��㡣 
	
	int lastPeriodStart = 0;
	for(x = 0; x < length; x++){
		if(daysInput[x].menstruationRecord == 1){
			lastPeriodStart = x;
		}
	}
	y = 0;
	int diff2 = 0;
	for(x = lastPeriodStart; x >= 0; x--){
		y = x - 1;
		for(y=x-1;y >=0; y--){
			if(daysInput[y].menstruationRecord == 1){
				break;
			}	
		}
		diff2 = getDaysDiff(&(daysInput[y].date),&(daysInput[x].date));
		if(diff2 > 300){
			//���̫�󣬹�ȥ�����ֱ�Ӻ���
			break; 
		}else{
			x = y+1;
		}
		
		
		
	}
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT----:call the al transV1TOV2DS_V2_Filer start 2 then use 1To2\n ");
	#endif
	LOGW("xxxxx->%d\n",x);
	x = x < 0? 0:x;
	return transV1TOV2DS_V2(length,daysInput+x,daysOutput,cycleOutput,userData,dayOutLength,cycleOutLength); 
} 

int changePastInfo_inputPeriod(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength,int shouldRecoveryCycle){
	//�ҵ������û�����ľ��ڿ�ʼ�;��ڽ��� 
	if(cycleInputLength <= 0){
		return ERROR_CODE;
	}
	int delayCount = 10;
	time_t ps = 0;
	time_t pe =0;
	int peReal = 0; 
	int x = 0;
	for(; x < dayInputlength; x++){
		if(daysInput[x].menstruationRecord == 255){
			ps = daysInput[x].date;
			break;
		}
	}
	for(x = dayInputlength-1; x >= 0; x--){
		if(daysInput[x].menstruationRecord == 254){
			pe = daysInput[x].date;
			peReal = 1;
			break;
		}
	}
	if(ps == 0){
	#ifdef JAVA_OUTPUT
		LOGW("ALLOG--changePastInfo the ps not find!\n");
	#endif
		return ERROR_CODE;
	}
	
	int tPs_pe = (pe-ps)/3600/24;
	if(tPs_pe > 15 || tPs_pe <= 0) { 
		pe = ps+3600*24*userData->averageMenstruationLength;
		peReal = 0;
	}
	cycleFlag* flag = initCycleFlag2();
	int checkThisNewPeriodIndex = 0;
	int setCheckIndex = 0;
	int cycleIndex = 0;
	// this is used to check if there are abnormal cycleData in this cycle array,
	// and we devide this two condition to do

	for(; cycleIndex < cycleInputLength; cycleIndex++){
		
		if(cycleIndex > 0 && cycleIndex < cycleInputLength - 1){
			time_t psBefore =  cycleInput[cycleIndex-1].cycleData[flag->menstruationStartConfirm]>EARLIST_TIME?
				      		cycleInput[cycleIndex-1].cycleData[flag->menstruationStartConfirm]:
					  		cycleInput[cycleIndex-1].cycleData[flag->menstruationStartForecast];
			time_t psthis   =  cycleInput[cycleIndex].cycleData[flag->menstruationStartConfirm]>EARLIST_TIME?
				      		cycleInput[cycleIndex].cycleData[flag->menstruationStartConfirm]:
					  		cycleInput[cycleIndex].cycleData[flag->menstruationStartForecast];
			time_t psNext   =  cycleInput[cycleIndex+1].cycleData[flag->menstruationStartConfirm]>EARLIST_TIME?
				      		cycleInput[cycleIndex+1].cycleData[flag->menstruationStartConfirm]:
					  		cycleInput[cycleIndex+1].cycleData[flag->menstruationStartForecast];
			
			int diff1  = getDaysDiff(&psBefore,&psthis);
			int diff2  = getDaysDiff(&psthis,&psNext);
			
			if(diff1 > LONGEST_CYCLE_LENGTH || diff2 > LONGEST_CYCLE_LENGTH || diff1 < SHORTEST_CYCLE_LENGTH || diff2 < SHORTEST_CYCLE_LENGTH){
				//use 150 to show if the user forget to put some cycle in her data
			
				break;
			}else{
				if(setCheckIndex == 0){
					int diff3  = getDaysDiff(&psBefore,&ps);
					int diff4  = getDaysDiff(&ps,&psthis);
					int diff5  = getDaysDiff(&ps,&psNext);
					if(diff3 < -15){
						checkThisNewPeriodIndex = cycleIndex - 2;
						setCheckIndex = 1;
					}else if(diff3 >= -15 && diff3 <= 15){
						checkThisNewPeriodIndex = cycleIndex-1;
						setCheckIndex  = 1;
					}else if(diff4 > 15 && diff3 > 15 ){
						//param 2 indic that insert a new cycle
						setCheckIndex = 2;
						checkThisNewPeriodIndex = cycleIndex;
					}else if(diff4 <= 15 && diff4 >= -15){
						//it indic ps is in psBefore cycle
						checkThisNewPeriodIndex = cycleIndex;
						setCheckIndex = 1;
					}else if(diff4 < -15 && diff5 > 15){
						//param 2 indic that insert a new cycle
						setCheckIndex = 2;
						checkThisNewPeriodIndex = cycleIndex+1;
					}else if(diff5 <= 15 && diff5 >= -15){
						setCheckIndex = 1;
						checkThisNewPeriodIndex = cycleIndex+1;
					}
				}
				
			}
		} 
		
	}
	int isNormal = 1;
	if(isNormal){
		//if all the cycles are right
		if(setCheckIndex == 1){
			if(checkThisNewPeriodIndex == -1){
				//it indic that we should add a new period before all the cycle;

				time_t psNext_1 = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
					      cycleInput[0].cycleData[flag->menstruationStartConfirm]:
						  cycleInput[0].cycleData[flag->menstruationStartForecast];
				throughPSPEAndNextPSGetCycle(ps,pe,peReal,cycleInput,userData,cycleOutput);
				int result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,delayCount);
				*cycleOutLength = 1;
				*dayOutLength = getDaysDiff(&ps,&psNext_1);
				int xx = delayCount;
				for(;xx > 0; xx--){
					daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
					daysOutput[xx-1].dayOfCycle = 0;
					daysOutput[xx-1].homePageConceptionChance = 0;
					daysOutput[xx-1].homePageMenstruationEnd = 0;
					daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
					daysOutput[xx-1].homePageOvulation=0;
					daysOutput[xx-1].periodAchieveConfirm=5;
					daysOutput[xx-1].periodAchieveForecast=5;
					daysOutput[xx-1].periodAvoidConfirm=4;
					daysOutput[xx-1].periodAvoidForecast=4;
				}
			}else{
				//this is used to change one cycle
				delayCount = 0;
				if(checkThisNewPeriodIndex == 0){
					delayCount = 10;
					cycleOutput[0].cycleNumber = cycleInput[checkThisNewPeriodIndex].cycleNumber;
					cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[checkThisNewPeriodIndex].ovulationDayUserRecordBasis;
					for(x = 0 ; x < 26 ; x++){
						cycleOutput[0].cycleData[x] = cycleInput[checkThisNewPeriodIndex].cycleData[x];
					}
					cycleOutput[0].cycleData[flag->menstruationStartConfirm] = ps;
					// peReal == 0?0:pe;
					if(peReal == 1){
						cycleOutput[0].cycleData[flag->menstruationEndConfirm] = pe;
					}
					cycleOutput[0].cycleData[flag->menstruationEndForecast] = pe;
					time_t psNext_1 = cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartConfirm]>100?
						      cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartConfirm]:
							  cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartForecast];
					*cycleOutLength = 1;
					
					int diff = getDaysDiff(&ps,&psNext_1);
				
					int result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,delayCount);
					int xx = delayCount;
					for(;xx > 0; xx--){
						daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
						daysOutput[xx-1].dayOfCycle = 0;
						daysOutput[xx-1].homePageConceptionChance = 0;
						daysOutput[xx-1].homePageMenstruationEnd = 0;
						daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
						daysOutput[xx-1].homePageOvulation=0;
						daysOutput[xx-1].periodAchieveConfirm=5;
						daysOutput[xx-1].periodAchieveForecast=5;
						daysOutput[xx-1].periodAvoidConfirm=4;
						daysOutput[xx-1].periodAvoidForecast=4;
					}
				}else{
					cycleOutput[0].cycleNumber = cycleInput[checkThisNewPeriodIndex-1].cycleNumber;
					cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[checkThisNewPeriodIndex-1].ovulationDayUserRecordBasis;
					for(x = 0 ; x < 26 ; x++){
						cycleOutput[0].cycleData[x] = cycleInput[checkThisNewPeriodIndex-1].cycleData[x];
					}
					cycleOutput[0].cycleData[flag->ovulationDayNextMenstruation] = ps - 14*24*3600;
					if(cycleOutput[0].cycleData[flag->ovulationDayConfirm] < 100 ){
						cycleOutput[0].cycleData[flag->ovulationDayConfirm] = ps - 14*24*3600;
					}else if(cycleOutput[0].cycleData[flag->ovulationDayBBTRise] < 100 && cycleOutput[0].cycleData[flag->ovulationDayUserRecord] < 100){
						cycleOutput[0].cycleData[flag->ovulationDayConfirm] = ps - 14*24*3600;
					}
					cycleOutput[1].cycleNumber = cycleInput[checkThisNewPeriodIndex].cycleNumber;
					cycleOutput[1].ovulationDayUserRecordBasis = cycleInput[checkThisNewPeriodIndex].ovulationDayUserRecordBasis;
					for(x = 0 ; x < 26 ; x++){
						cycleOutput[1].cycleData[x] = cycleInput[checkThisNewPeriodIndex].cycleData[x];
					}
					cycleOutput[1].cycleData[flag->menstruationStartConfirm] = ps;
					if(peReal){
						cycleOutput[1].cycleData[flag->menstruationEndConfirm] = pe;
					}
					cycleOutput[1].cycleData[flag->menstruationEndForecast] = pe;
					time_t psNext_1 = cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartConfirm]>100?
						      cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartConfirm]:
							  cycleInput[checkThisNewPeriodIndex+1].cycleData[flag->menstruationStartForecast];
					*cycleOutLength = 2;
					time_t ps0 = cycleOutput[0].cycleData[flag->menstruationStartConfirm]>100?
						      cycleOutput[0].cycleData[flag->menstruationStartConfirm]:
							  cycleOutput[0].cycleData[flag->menstruationStartForecast];
					int diff = getDaysDiff(&ps,&psNext_1);
					int diff2 = getDaysDiff(&ps0,&ps);
					int result = throughLastCycleGetDaysData(cycleOutput,ps,daysOutput,delayCount);
					result = throughLastCycleGetDaysData(cycleOutput+1,psNext_1,daysOutput,delayCount+diff2);
					*dayOutLength = diff+diff2;
				}
				
				
				//return result;
				
			} 
		}else if (setCheckIndex == 2){
			//this is used to insert a new cycle
			int x = 0;
			delayCount = 0;
			for(; x< checkThisNewPeriodIndex; x++){
				cycleOutput[x].cycleNumber = cycleInput[x].cycleNumber-1;
				cycleOutput[x].ovulationDayUserRecordBasis = cycleInput[x].ovulationDayUserRecordBasis;
				int y = 0;
				for(y = 0 ; y < 26 ; y++){
					cycleOutput[x].cycleData[y] = cycleInput[x].cycleData[y];
				}
				
			}
			time_t ovLast = ps - 14*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->ovulationDayNextMenstruation] = ovLast;
	
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->dangerWindowEndConfirm] = ovLast+4*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->dangerWindowEndForecast] = ovLast+4*24*3600;
	
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->fertileWindowStartConfirm] = ovLast-5*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->fertileWindowEndConfirm] = ovLast + 1*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->fertileWindowStartForecast] = ovLast-5*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->fertileWindowEndForecast] = ovLast+1*24*3600;
			cycleOutput[checkThisNewPeriodIndex-1].cycleData[flag->ovulationDayConfirm] = ovLast;
			
			
			
			
			
			
			
			cycleOutput[checkThisNewPeriodIndex].cycleNumber = cycleInput[checkThisNewPeriodIndex-1].cycleNumber;
			cycleOutput[checkThisNewPeriodIndex].ovulationDayUserRecordBasis = cycleInput[checkThisNewPeriodIndex-1].ovulationDayUserRecordBasis;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->menstruationStartConfirm] = ps;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->menstruationEndConfirm] = peReal == 0?0:pe;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->menstruationStartForecast] = ps;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->menstruationEndForecast] = pe;
			time_t psNext_1 = cycleInput[checkThisNewPeriodIndex].cycleData[flag->menstruationStartConfirm]>100?
				      cycleInput[checkThisNewPeriodIndex].cycleData[flag->menstruationStartConfirm]:
					  cycleInput[checkThisNewPeriodIndex].cycleData[flag->menstruationStartForecast];
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->BBTRiseDay] = 0;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->dangerWindowEndConfirm] = 0;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->dangerWindowEndForecast] = psNext_1 - 10*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->dangerWindowStartConfirm] = 0;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->dangerWindowStartForecast] = pe + 24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->fertileWindowEndConfirm] = psNext_1 - 13*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->fertileWindowEndForecast] = psNext_1 - 13*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->fertileWindowStartConfirm] = psNext_1 - 19*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->fertileWindowStartForecast] = psNext_1 - 19*24*3600;

			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextDangerWindowEndForecast] = psNext_1 + (userData->averageCycleLength-11)*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextDangerWindowStartForecast] = psNext_1 + (userData->averageMenstruationLength)*24*3600;;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextFertileWindowEndForecast] = psNext_1 + (userData->averageCycleLength-13)*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextFertileWindowStartForecast] = psNext_1 + (userData->averageCycleLength-19)*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextMenstruationEndForecast] = psNext_1+userData->averageMenstruationLength*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextMenstruationStartForecast] = psNext_1;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->nextOvulationDayForecast] = psNext_1 + (userData->averageCycleLength-14)*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->ovulationDayBBTRise] = 0;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->ovulationDayConfirm] = psNext_1 - 14*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->ovulationDayForecast] = psNext_1 - 14*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->ovulationDayNextMenstruation] = psNext_1 - 14*24*3600;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->ovulationDayUserRecord] = 0;
			cycleOutput[checkThisNewPeriodIndex].cycleData[flag->peakDay] = 0;
			time_t psBe = cycleInput[checkThisNewPeriodIndex-1].cycleData[flag->menstruationStartConfirm]>100?
						  cycleInput[checkThisNewPeriodIndex-1].cycleData[flag->menstruationStartConfirm]:
						  cycleInput[checkThisNewPeriodIndex-1].cycleData[flag->menstruationStartForecast];
			*cycleOutLength = checkThisNewPeriodIndex+1;
			int length1= getDaysDiff(&ps,&psNext_1);
			int length2 = getDaysDiff(&psBe,&ps);
			*dayOutLength = length1+length2;
			
			int result1 = throughLastCycleGetDaysData(cycleOutput+checkThisNewPeriodIndex-1,ps,daysOutput,delayCount);
			int result2 = throughLastCycleGetDaysData(cycleOutput+checkThisNewPeriodIndex,psNext_1,daysOutput,length2+delayCount);
			//return result1+result2;
		}else if(setCheckIndex == 0){
			//if this value is 0, there were two possibilitys
			if(cycleInputLength < 3){
				if(cycleInputLength == 2){
					time_t ps0 =  cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
				      				cycleInput[0].cycleData[flag->menstruationStartConfirm]:
					  				cycleInput[0].cycleData[flag->menstruationStartForecast];
					time_t ps1 =  cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
				      				cycleInput[1].cycleData[flag->menstruationStartConfirm]:
					  				cycleInput[1].cycleData[flag->menstruationStartForecast];
					int diff1 = getDaysDiff(&ps,&ps0);
					int diff2 = getDaysDiff(&ps,&ps1);
					if(diff1 > 15){
						//it indic that insert a new cycle before the first cycle
						throughPSPEAndNextPSGetCycle(ps,pe,peReal,cycleInput,userData,cycleOutput);
						throughLastCycleGetDaysData(cycleOutput,ps0,daysOutput,delayCount);
						*cycleOutLength = 1;
						*dayOutLength = diff1;
						int xx = delayCount;
						for(;xx > 0; xx--){
							daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
							daysOutput[xx-1].dayOfCycle = 0;
							daysOutput[xx-1].homePageConceptionChance = 0;
							daysOutput[xx-1].homePageMenstruationEnd = 0;
							daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
							daysOutput[xx-1].homePageOvulation=0;
							daysOutput[xx-1].periodAchieveConfirm=5;
							daysOutput[xx-1].periodAchieveForecast=5;
							daysOutput[xx-1].periodAvoidConfirm=4;
							daysOutput[xx-1].periodAvoidForecast=4;
						}
						*dayOutLength += delayCount;
					}else if(diff1 >= -15 && diff1 <= 15){
						//it indic that update the first cycle
						//this is used to change one cycle
						delayCount = 10;
						cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber;
						cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
						for(x = 0 ; x < 26 ; x++){
							cycleOutput[0].cycleData[x] = cycleInput[0].cycleData[x];
						}
						cycleOutput[0].cycleData[flag->menstruationStartConfirm] = ps;
						if(peReal){
							cycleOutput[0].cycleData[flag->menstruationEndConfirm] = pe;
						}
											
						cycleOutput[0].cycleData[flag->menstruationEndForecast] = pe;
						*cycleOutLength = 1;
						*dayOutLength = diff2;
						int result = throughLastCycleGetDaysData(cycleOutput,ps1,daysOutput,delayCount);
						int xx = delayCount;
						for(;xx > 0; xx--){
							daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
							daysOutput[xx-1].dayOfCycle = 0;
							daysOutput[xx-1].homePageConceptionChance = 0;
							daysOutput[xx-1].homePageMenstruationEnd = 0;
							daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
							daysOutput[xx-1].homePageOvulation=0;
							daysOutput[xx-1].periodAchieveConfirm=5;
							daysOutput[xx-1].periodAchieveForecast=5;
							daysOutput[xx-1].periodAvoidConfirm=4;
							daysOutput[xx-1].periodAvoidForecast=4;
						}
						*dayOutLength += delayCount;	
					}else if(diff1 < -15 && diff2 > 15){
						//it indic that insert a new cycle between 0 and 1
						delayCount = 0;
						cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber-1;
						cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
						for(x = 0 ; x < 26 ; x++){
							cycleOutput[0].cycleData[x] = cycleInput[0].cycleData[x];
						}
						cycleOutput[0].cycleData[flag->ovulationDayNextMenstruation] = ps - 14*24*3600;
						cycleOutput[0].cycleData[flag->ovulationDayConfirm] = ps - 14*24*3600;
						throughPSPEAndNextPSGetCycle(ps,pe,peReal,cycleInput+1,userData,cycleOutput+1);
						int result = throughLastCycleGetDaysData(cycleOutput,ps,daysOutput,delayCount);
						result = throughLastCycleGetDaysData(cycleOutput+1,ps1,daysOutput,delayCount-diff1);
						*cycleOutLength = 2;
						*dayOutLength = diff2-diff1;
					}else{
						return ERROR_CODE;
					}
				}else if(cycleInputLength == 1){
					time_t ps0 =  cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
				      				cycleInput[0].cycleData[flag->menstruationStartConfirm]:
					  				cycleInput[0].cycleData[flag->menstruationStartForecast];
					int diff1 = getDaysDiff(&ps,&ps0);
					if(diff1 > 15){
						//it indic that insert a new cycle before the first cycle
						throughPSPEAndNextPSGetCycle(ps,pe,peReal,cycleInput,userData,cycleOutput);
						throughLastCycleGetDaysData(cycleOutput,ps0,daysOutput,delayCount);
						*cycleOutLength = 1;
						*dayOutLength = diff1;
						int xx = delayCount;
						for(;xx > 0; xx--){
							daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
							daysOutput[xx-1].dayOfCycle = 0;
							daysOutput[xx-1].homePageConceptionChance = 0;
							daysOutput[xx-1].homePageMenstruationEnd = 0;
							daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
							daysOutput[xx-1].homePageOvulation=0;
							daysOutput[xx-1].periodAchieveConfirm=5;
							daysOutput[xx-1].periodAchieveForecast=5;
							daysOutput[xx-1].periodAvoidConfirm=4;
							daysOutput[xx-1].periodAvoidForecast=4;
						}
						*dayOutLength += delayCount;
					} else {
						//if only one cycle ,the last operation is should not in here
						return ERROR_CODE;
					}
					
				}
			} else {
				return ERROR_CODE;
			}
		}
		 
	}else{
		return ERROR_CODE;
	}
	if(shouldRecoveryCycle == 1){
		//we should recovery the first cycle
		int xx = 0;
		cycleOutput[*cycleOutLength].cycleNumber = cycleInput[0].cycleNumber;
		cycleOutput[*cycleOutLength].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
		for(;xx < 26;xx++){
			cycleOutput[*cycleOutLength].cycleData[xx] = cycleInput[0].cycleData[xx];
		}
		*cycleOutLength += 1;
	}
	*dayOutLength += delayCount;
/*	
	int xx = delayCount;
	for(;xx > 0; xx--){
		daysOutput[xx-1].date = daysOutput[delayCount].date-(delayCount+1-xx)*24*3600;
		daysOutput[xx-1].dayOfCycle = 0;
		daysOutput[xx-1].homePageConceptionChance = 0;
		daysOutput[xx-1].homePageMenstruationEnd = 0;
		daysOutput[xx-1].homePageNextMenstruation = delayCount+1-xx;
		daysOutput[xx-1].homePageOvulation=0;
		daysOutput[xx-1].periodAchieveConfirm=5;
		daysOutput[xx-1].periodAchieveForecast=5;
		daysOutput[xx-1].periodAvoidConfirm=4;
		daysOutput[xx-1].periodAvoidForecast=4;
	}
*/	
	
	
	#ifdef TEST_ALL_FILE
	int indexC = 0;
	
	
		for(;indexC < *cycleOutLength;indexC++){
			printAllCycleUnit(cycleOutput[indexC]);
		}
		//LOGW("\n------------ALDayOutputList days int---------------\n");
		int pr = 0;
		for(;pr <*dayOutLength ; pr++){
			printAllDayOutputUnit(daysOutput[pr]);
		}   
		#endif
		return 0;
	
	
				
				
}
int deleteDaysOutput(time_t start,time_t end,dayUnitDSOutput *daysOutput,int deleteFirstCycleFore10daysData){
	if(start > end || start < 1000000 || end < 1000000){
		return ERROR_CODE;
	}
	int diff = getDaysDiff(&start, &end);
	int x = 0;
	for(; x < diff-deleteFirstCycleFore10daysData; x++){
		daysOutput[x].date = start+x*24*3600;
		daysOutput[x].periodAchieveForecast = 99;
	}
	for(x = diff-deleteFirstCycleFore10daysData; x < diff ;x++ ){
		daysOutput[x].date = start+x*24*3600;
		daysOutput[x].homePageNextMenstruation = diff - x;
		daysOutput[x].homePageConceptionChance = 0;
		daysOutput[x].homePageOvulation=0;
		daysOutput[x].periodAchieveConfirm=5;
		daysOutput[x].periodAchieveForecast=5;
		daysOutput[x].periodAvoidConfirm=4;
		daysOutput[x].periodAvoidForecast=4;
	}
	for(x = diff; x < diff + deleteFirstCycleFore10daysData;x++){
		daysOutput[x].date = start - (x+1-diff)*24*3600;
		daysOutput[x].periodAchieveForecast = 99;
	}
	
	return diff;
}

int findWhichCycleDelete(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength){
	cycleFlag* flag = initCycleFlag2();
	int x = 0;
	for(;x < dayInputlength; x++){
		if(daysInput[x].menstruationRecord == 255){
			break;
		}
	}
	time_t ps = daysInput[x].date;
	for(x = 0; x < cycleInputLength;x++ ){
		if(cycleInput[x].cycleData[flag->menstruationStartConfirm] == ps){
			return x;
			break;
		}
	}
	return -1;
	
}
int changePastInfo_deletePeriod(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	
	int deleteFirstCycleFore10daysData = 10;
	int deleteCycleIndex = findWhichCycleDelete(dayInputlength,daysInput,cycleInput,cycleInputLength);
	if(cycleInputLength<2) return ERROR_CODE;

	cycleFlag* flag = initCycleFlag2();
	
	if(deleteCycleIndex == 0){
		
		time_t psNext = cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
					cycleInput[1].cycleData[flag->menstruationStartConfirm]:
					cycleInput[1].cycleData[flag->menstruationStartForecast];
		time_t psNow = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
					cycleInput[0].cycleData[flag->menstruationStartConfirm]:
					cycleInput[0].cycleData[flag->menstruationStartForecast];
		*dayOutLength = deleteDaysOutput(psNow,psNext,daysOutput,deleteFirstCycleFore10daysData);
		int x = 1;
		for(;x< cycleInputLength; x++){
			cycleOutput[x-1].cycleNumber = cycleInput[x].cycleNumber;
			cycleOutput[x-1].ovulationDayUserRecordBasis = cycleInput[x].ovulationDayUserRecordBasis;
			int y = 0;
			for(; y < 26;y++){
				cycleOutput[x-1].cycleData[y] = cycleInput[x].cycleData[y];
			}
		}
		*cycleOutLength = cycleInputLength - 1; 
	}else if(deleteCycleIndex < cycleInputLength-1){
		//autu connect deleteCycleIndex -1 and deleteCycleIndex to be a new cycle
		time_t psNext = cycleInput[deleteCycleIndex+1].cycleData[flag->menstruationStartConfirm]>100?
					cycleInput[deleteCycleIndex+1].cycleData[flag->menstruationStartConfirm]:
					cycleInput[deleteCycleIndex+1].cycleData[flag->menstruationStartForecast];
		time_t psNew = cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationStartConfirm]>100?
					cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationStartConfirm]:
					cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationStartForecast];
		int peReal = 0;
		time_t peNew = 0;	
		if(cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationEndConfirm] > 100){
			peNew = cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationEndConfirm];
			peReal = 1;
		}else{
			peNew = cycleInput[deleteCycleIndex-1].cycleData[flag->menstruationEndForecast];
			int diffs = getDaysDiff(&psNew,&peNew);
			if(diffs < 2){
				peNew = psNew+5*24*3600;
			}
		}
		//throughPSPEAndNextPSGetCycle(psNew,peNew,peReal,cycleInput+deleteCycleIndex+1,userData,cycleOutput+deleteCycleIndex-1);			 
	
		
		cycleOutput[deleteCycleIndex-1].cycleNumber = cycleInput[deleteCycleIndex].cycleNumber;
		cycleOutput[deleteCycleIndex-1].ovulationDayUserRecordBasis = cycleInput[deleteCycleIndex-1].ovulationDayUserRecordBasis;
		int y = 0;
		for(; y < 26;y++){
			cycleOutput[deleteCycleIndex-1].cycleData[y] = cycleInput[deleteCycleIndex-1].cycleData[y];
		}
		cycleOutput[deleteCycleIndex-1].cycleData[flag->ovulationDayNextMenstruation] = psNext - 14*24*3600;
		cycleOutput[deleteCycleIndex-1].cycleData[flag->ovulationDayForecast] = psNext - 14*24*3600;
		cycleOutput[deleteCycleIndex-1].cycleData[flag->ovulationDayConfirm] = psNext - 14*24*3600;
		
		throughLastCycleGetDaysData(cycleOutput+deleteCycleIndex-1,psNext,daysOutput,0);
		int x = 0;
		for(;x< deleteCycleIndex-1; x++){
			cycleOutput[x].cycleNumber = cycleInput[x].cycleNumber+1;
			cycleOutput[x].ovulationDayUserRecordBasis = cycleInput[x].ovulationDayUserRecordBasis;
			int y = 0;
			for(; y < 26;y++){
				cycleOutput[x].cycleData[y] = cycleInput[x].cycleData[y];
			}
		}
		for(x = deleteCycleIndex;x< cycleInputLength - 1; x++){
			cycleOutput[x].cycleNumber = cycleInput[x+1].cycleNumber;
			cycleOutput[x].ovulationDayUserRecordBasis = cycleInput[x+1].ovulationDayUserRecordBasis;
			int y = 0;
			for(; y < 26;y++){
				cycleOutput[x].cycleData[y] = cycleInput[x+1].cycleData[y];
			}
		}
		*cycleOutLength = cycleInputLength-1;
		*dayOutLength = getDaysDiff(&psNew,&psNext);
		
	}else {
		return ERROR_CODE;
	}	
	#ifdef TEST_ALL_FILE
	int indexC = 0;


	for(;indexC < *cycleOutLength+2;indexC++){
		printAllCycleUnit(cycleOutput[indexC]);
	}
	//LOGW("\n------------ALDayOutputList days int---------------\n");
	int pr = 0;
	for(;pr <*dayOutLength ; pr++){
		printAllDayOutputUnit(daysOutput[pr]);
	}   

	#endif	
	return 1;			
}






//int changePastInfo_deletePeriod(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
//					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
//	if(cycleInputLength != 3){
//		if(cycleInputLength == 2){
//			//˵���û�������Ǹ�����û��֮ǰ�����
//			cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber;
//			cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
//			int x ;
//			for(x = 0 ; x < 26 ; x++){
//				cycleOutput[0].cycleData[x] = cycleInput[0].cycleData[x];
//			}
//			cycleFlag* flag = initCycleFlag2();
//			cycleOutput[0].cycleData[flag->menstruationStartConfirm] = 0;
//			cycleOutput[0].cycleData[flag->menstruationEndConfirm] = 0;
//			time_t psNext_1 = cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
//				      cycleInput[1].cycleData[flag->menstruationStartConfirm]:
//					  cycleInput[1].cycleData[flag->menstruationStartForecast];
//			time_t ps = cycleOutput[0].cycleData[flag->menstruationStartForecast];
//			int diff = getDaysDiff(&ps,&psNext_1);
//			int result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
//			*cycleOutLength = 1;
//			*dayOutLength = diff;
//			#ifdef TEST_ALL_FILE
//			int indexC = 0;
//	
//	
//			for(;indexC < *cycleOutLength;indexC++){
//				printAllCycleUnit(cycleOutput[indexC]);
//			}
//			//LOGW("\n------------ALDayOutputList days int---------------\n");
//			int pr = 0;
//			for(;pr <*dayOutLength ; pr++){
//				printAllDayOutputUnit(daysOutput[pr]);
//			}   
//			return 1;
//		#endif
//		}
//	#ifdef JAVA_OUTPUT
//		LOGW("ALLOG--changePastInfo the cycleInput is wrong!\n");
//	#endif
//		return ERROR_CODE;
//	}
//	cycleOutput[0].cycleNumber = cycleInput[1].cycleNumber;
//	cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[1].ovulationDayUserRecordBasis;
//	int x;
//	for(x = 0 ; x < 26 ; x++){
//		cycleOutput[0].cycleData[x] = cycleInput[1].cycleData[x];
//	}
//	cycleFlag* flag = initCycleFlag2();	
//	time_t ps = cycleOutput[0].cycleData[flag->menstruationStartForecast];
//	cycleOutput[0].cycleData[flag->menstruationStartConfirm] = 0; 	
//	cycleOutput[0].cycleData[flag->menstruationEndConfirm] = 0;
//	throughLastCycleGetDaysData(cycleInput,ps,daysOutput,0);
//	time_t psNext = cycleInput[2].cycleData[flag->menstruationStartConfirm]>100?
//				      cycleInput[2].cycleData[flag->menstruationStartConfirm]:
//					  cycleInput[2].cycleData[flag->menstruationStartForecast];
//	
//	time_t psInput = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
//				     cycleInput[0].cycleData[flag->menstruationStartConfirm]:
//					 cycleInput[0].cycleData[flag->menstruationStartForecast];
//	int diff = getDaysDiff(&psInput,&ps);
//	throughLastCycleGetDaysData(cycleOutput,psNext,daysOutput,diff);
//	
//	*cycleOutLength = 1;
//	*dayOutLength = 2*diff;
//	
//	#ifdef TEST_ALL_FILE
//	int indexC = 0;
//		
//	for(;indexC < *cycleOutLength;indexC++){
//		printAllCycleUnit(cycleOutput[indexC]);
//	}
//	//LOGW("\n------------ALDayOutputList days int---------------\n");
//	int pr = 0;
//	for(;pr <*dayOutLength ; pr++){
//		printAllDayOutputUnit(daysOutput[pr]);
//	}   
//	#endif
//	return 1;			
//}

int changePastInfo_inputBBT(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	*dayOutLength = 0;
	*cycleOutLength = 0; 
	int bbtlength = 0;
	int x = 0;
	BBTUnitDS BBTArray[100];
	for(;x < dayInputlength;x++){
		if(daysInput[x].tempFlag == 0 && daysInput[x].BBT > 34 && daysInput[x].BBT < 42) {
			BBTArray[bbtlength].BBT = daysInput[x].BBT;
			BBTArray[bbtlength].index = x;
			bbtlength++;
		}
	}
	cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber;
	cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
	for(x = 0 ; x < 26 ; x++){
		cycleOutput[0].cycleData[x] = cycleInput[0].cycleData[x];
	}
	cycleFlag* flag = initCycleFlag2();	
	time_t psNext_1 = cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
				      cycleInput[1].cycleData[flag->menstruationStartConfirm]:
					  cycleInput[1].cycleData[flag->menstruationStartForecast]; 
	time_t ps = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
				cycleInput[0].cycleData[flag->menstruationStartConfirm]:
				cycleInput[0].cycleData[flag->menstruationStartForecast];
	int diff = getDaysDiff(&ps,&psNext_1);	
	int result = -1;			  
	int ovNext = 0;
	int ov = BBTTestOvulationDay(BBTArray,bbtlength,&ovNext);
	if(ov != -1 ){
		if(cycleInput[0].cycleData[flag->BBTRiseDay] != daysInput[ov].date){
			//˵������һ���µ����������
			cycleOutput[0].cycleData[flag->BBTRiseDay] = daysInput[ov].date;
			if(cycleInput[0].cycleData[flag->ovulationDayUserRecord] > 100){
				*dayOutLength = 0;
				*cycleOutLength = 1;
				result = 1;
			}else{
				cycleOutput[0].cycleData[flag->ovulationDayConfirm] = daysInput[ov].date;
				cycleOutput[0].cycleData[flag->ovulationDayBBTRise] = daysInput[ov].date;
				throughtOVGetFertileAndDanger(cycleOutput,1);
				result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
				*dayOutLength = diff;
				*cycleOutLength = 1;;
			} 
		}else{
			return 1;
		}	
	}else{
		if(cycleInput[0].cycleData[flag->BBTRiseDay] > 100){
			// ˵���˻�������������Ҫ��ɾ��
			if(cycleInput[0].cycleData[flag->ovulationDayUserRecord] > 100){
				cycleOutput[0].cycleData[flag->BBTRiseDay] = 0;
				cycleOutput[0].cycleData[flag->ovulationDayBBTRise] = 0;
				*dayOutLength = 0;
				*cycleOutLength = 1;
				result = 1;
			}else{
				if(cycleInput[0].cycleData[flag->ovulationDayNextMenstruation] > 100){
					cycleOutput[0].cycleData[flag->ovulationDayConfirm] = cycleInput[0].cycleData[flag->ovulationDayNextMenstruation];
					throughtOVGetFertileAndDanger(cycleOutput,1);
					result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
					*dayOutLength = diff;
					*cycleOutLength = 1;
				}else{
					cycleOutput[0].cycleData[flag->ovulationDayNextMenstruation] = psNext_1 - 14*24*3600;
					
					cycleOutput[0].cycleData[flag->ovulationDayConfirm] = cycleInput[0].cycleData[flag->ovulationDayNextMenstruation];
					throughtOVGetFertileAndDanger(cycleOutput,1);
					result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
					*dayOutLength = diff;
					*cycleOutLength = 1;
				}
			} 
		}else{
			return 1;
		}	
	}
	#ifdef TEST_ALL_FILE
	int indexC = 0;
		
	for(;indexC < *cycleOutLength;indexC++){
		printAllCycleUnit(cycleOutput[indexC]);
	}
	//LOGW("\n------------ALDayOutputList days int---------------\n");
	int pr = 0;
	for(;pr <*dayOutLength ; pr++){
		printAllDayOutputUnit(daysOutput[pr]);
	}   
	#endif
	return result;					
}

int changePastInfo_inputRecord(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	*dayOutLength = 0;
	*cycleOutLength = 0; 
	int x = 0;
	for(;x<dayInputlength;x++){
		if(daysInput[x].ovulationTestResult != 0) break;
	}
	cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber;
	cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
	int y = 0;
	for(y = 0 ; y < 26 ; y++){
		cycleOutput[0].cycleData[y] = cycleInput[0].cycleData[y];
	}
	cycleFlag* flag = initCycleFlag2();	
	time_t psNext_1 = cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
				      cycleInput[1].cycleData[flag->menstruationStartConfirm]:
					  cycleInput[1].cycleData[flag->menstruationStartForecast];
	time_t ps = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
				cycleInput[0].cycleData[flag->menstruationStartConfirm]:
				cycleInput[0].cycleData[flag->menstruationStartForecast];
	int diff = getDaysDiff(&ps,&psNext_1);	
	int result = -1;			  
	if(daysInput[x].ovulationTestResult == 2){
		//˵������һ���µ�ȷ�������յ�����
		cycleOutput[0].cycleData[flag->ovulationDayUserRecord] = daysInput[x].date;
		cycleOutput[0].cycleData[flag->ovulationDayConfirm] = daysInput[x].date;
		throughtOVGetFertileAndDanger(cycleOutput,1);
		result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
		
		*cycleOutLength = 1;
		*dayOutLength = diff;
		
		
	}else if(daysInput[x].ovulationTestResult == 4){
		cycleOutput[0].cycleData[flag->ovulationDayUserRecord] = 0;
		if(cycleOutput[0].cycleData[flag->BBTRiseDay] > 100){
			cycleOutput[0].cycleData[flag->ovulationDayConfirm] = cycleOutput[0].cycleData[flag->BBTRiseDay];
			throughtOVGetFertileAndDanger(cycleOutput,1);
			result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
			*cycleOutLength = 1;
			*dayOutLength = diff;
		}else{
			if(cycleInput[0].cycleData[flag->ovulationDayNextMenstruation] > 100){
				
				cycleOutput[0].cycleData[flag->ovulationDayConfirm] = cycleInput[0].cycleData[flag->ovulationDayNextMenstruation];
				throughtOVGetFertileAndDanger(cycleOutput,1);
				result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
				*cycleOutLength = 1;
				*dayOutLength = diff;
			}else{
				cycleOutput[0].cycleData[flag->ovulationDayNextMenstruation] = psNext_1 - 14*24*3600;
				cycleOutput[0].cycleData[flag->ovulationDayConfirm] = cycleInput[0].cycleData[flag->ovulationDayNextMenstruation];
				throughtOVGetFertileAndDanger(cycleOutput,1);
				result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysOutput,0);
				*cycleOutLength = 1;
				*dayOutLength = diff;
			}
		}
	}
	#ifdef TEST_ALL_FILE
	int indexC = 0;
		
	for(;indexC < *cycleOutLength;indexC++){
		printAllCycleUnit(cycleOutput[indexC]);
	}
	//LOGW("\n------------ALDayOutputList days int---------------\n");
	int pr = 0;
	for(;pr <*dayOutLength ; pr++){
		printAllDayOutputUnit(daysOutput[pr]);
	}   
	#endif
	return result;						
}

int fixOldPE(time_t peRight,time_t peWrong,cycleUnitDS *cycleInput,cycleUnitDS *cycleOutput,int *cycleOutputLength,
				userUnitDS *userData,dayUnitDSOutput *daysData,int *daysDataLength){
	if(!daysData) return ERROR_CODE;
	if(!cycleOutput) return ERROR_CODE;
	#ifdef JAVA_OUTPUT
	LOGW("ALOUTPUT-----:fixPE start!\n");
	#endif
	cycleFlag* flag = initCycleFlag2();
	cycleOutput[0].ovulationDayUserRecordBasis = cycleInput[0].ovulationDayUserRecordBasis;
	cycleOutput[0].cycleNumber = cycleInput[0].cycleNumber;
	int copyIndex = 0;
	for(;copyIndex < cycleFlagLength;copyIndex++){
		cycleOutput[0].cycleData[copyIndex] = cycleInput[0].cycleData[copyIndex];
	}
	int result = 0;
	
	int dayDiff = getDaysDiff(cycleOutput[0].cycleData+flag->menstruationStartConfirm,&peRight);
	if(dayDiff < 20  && dayDiff > 1){
		time_t tNULL =0;
		setCalenderAfterNDays(&peRight,0,cycleOutput[0].cycleData+flag->menstruationEndConfirm);
		time_t psNext_1 = cycleInput[1].cycleData[flag->menstruationStartConfirm]>100?
				      cycleInput[1].cycleData[flag->menstruationStartConfirm]:
					  cycleInput[1].cycleData[flag->menstruationStartForecast];
		time_t ps = cycleInput[0].cycleData[flag->menstruationStartConfirm]>100?
				cycleInput[0].cycleData[flag->menstruationStartConfirm]:
				cycleInput[0].cycleData[flag->menstruationStartForecast];
		*cycleOutputLength = 1;
		*daysDataLength = getDaysDiff(&ps,&psNext_1);
		result = throughLastCycleGetDaysData(cycleOutput,psNext_1,daysData,0);
	#ifdef TEST_ALL_FILE
		int indexC = 0;
		for(;indexC < cycleOutputLength;indexC++){
			printAllCycleUnit(cycleOutput[indexC]);
		}
		//LOGW("\n------------ALDayOutputList days int---------------\n");
		int pr = 0;
		for(;pr <*daysDataLength ; pr++){
			printAllDayOutputUnit(daysData[pr]);
		}   
		#endif
	}
	
	
	return result; 
}


int changePastInfo_inputPeriod_filer(int dayInputlength,dayUnitDSInput *daysInput,cycleUnitDS *cycleInput,int cycleInputLength,dayUnitDSOutput *daysOutput,
					cycleUnitDS *cycleOutput,userUnitDS *userData,int *dayOutLength,int *cycleOutLength){
	cycleFlag* flag = initCycleFlag2();
	int cycleIndex = 0;
	int abNormalIndex[10] = {0};
	int abNormalCount = 0;
	for(cycleIndex = 0; cycleIndex < cycleInputLength; cycleIndex++){
		if(cycleInput[cycleIndex].cycleData[flag->menstruationStartConfirm] < EARLIST_TIME && 
   			cycleInput[cycleIndex].cycleData[flag->menstruationStartForecast] < EARLIST_TIME){
			abNormalIndex[abNormalCount] = cycleIndex;
			abNormalCount++;
		}
	}
	if(abNormalCount > 0){
		//����ж�һ������
		if(abNormalCount == 1){
			if(abNormalIndex[0] == 0 && cycleInputLength > 1){
				
				time_t ps_Next = cycleInput[1].cycleData[flag->menstruationStartConfirm] > EARLIST_TIME?
									cycleInput[1].cycleData[flag->menstruationStartConfirm]:
										cycleInput[1].cycleData[flag->menstruationStartForecast];
				int x = 0;
				for(;x<dayInputlength;x++){
					if(daysInput[x].menstruationRecord> 0 && daysInput[x].menstruationRecord<200){
						int diff = getDaysDiff(&(daysInput[x].date),&ps_Next);
						if(diff < 2*userData->averageCycleLength && diff > SHORTEST_CYCLE_LENGTH ){
							int y = 0;
							for(y = x;y < dayInputlength; y++){
								int diff2 = getDaysDiff(&(daysInput[x].date),&(daysInput[y+1].date));
								if(diff2 > MOST_PERIOD_LENGTH){
									//this y is the end!
									break;
								}
							}
							int diff2 = getDaysDiff(&(daysInput[x].date),&(daysInput[y].date));
							time_t pe = 0;
							int peReal = 0;
							if(diff2 <= 2){
								pe = daysInput[x].date+userData->averageMenstruationLength*24*3600;
							}else{
								pe = daysInput[y].date;
								peReal = 1;
							}
 							throughPSPEAndNextPSGetCycle(daysInput[x].date,pe,peReal,cycleInput+1,userData,cycleInput);
 							return changePastInfo_inputPeriod(dayInputlength,daysInput,cycleInput,cycleInputLength,daysOutput,
									cycleOutput,userData,dayOutLength,cycleOutLength,1);
						}
					}
				}
			}else if(abNormalIndex[0] == 0 && cycleInputLength == 1){
				return ERROR_CODE;
			} 	
		}else{
			return ERROR_CODE;
		}
		
		 
	}else{
		return changePastInfo_inputPeriod(dayInputlength,daysInput,cycleInput,cycleInputLength,daysOutput,
					cycleOutput,userData,dayOutLength,cycleOutLength,0);	
	} 
	return 0;
							
}
int throughPSPEAndNextPSGetCycle(time_t ps,time_t pe,int peReal,cycleUnitDS *nextCycleInput,userUnitDS *userData,cycleUnitDS *cycleInput){
	cycleFlag* flag = initCycleFlag2();
	cycleInput[0].cycleNumber = nextCycleInput[0].cycleNumber-1;
	cycleInput[0].ovulationDayUserRecordBasis = nextCycleInput[0].ovulationDayUserRecordBasis;
	cycleInput[0].cycleData[flag->menstruationStartConfirm] = ps;
	cycleInput[0].cycleData[flag->menstruationEndConfirm] = peReal == 1?pe:0;
	cycleInput[0].cycleData[flag->menstruationStartForecast] = ps;
	cycleInput[0].cycleData[flag->menstruationEndForecast] = pe;
	time_t psNext_1 = 0;
	int psNextReal = 0;
	if(nextCycleInput[0].cycleData[flag->menstruationStartConfirm]>100){
		psNext_1 = nextCycleInput[0].cycleData[flag->menstruationStartConfirm];
		psNextReal = 1;
	}else{
		psNext_1 = nextCycleInput[0].cycleData[flag->menstruationStartForecast];
	}
	
	cycleInput[0].cycleData[flag->BBTRiseDay] = 0;
	cycleInput[0].cycleData[flag->dangerWindowEndConfirm] = 0;
	cycleInput[0].cycleData[flag->dangerWindowEndForecast] = psNextReal == 1? psNext_1 - 10*24*3600:ps + (userData->averageCycleLength-10)*24*3600;
	cycleInput[0].cycleData[flag->dangerWindowStartConfirm] = pe + 24*3600;
	cycleInput[0].cycleData[flag->dangerWindowStartForecast] = pe + 24*3600;
	cycleInput[0].cycleData[flag->fertileWindowEndConfirm] = psNext_1 - 13*24*3600;
	cycleInput[0].cycleData[flag->fertileWindowEndForecast] = psNext_1 - 13*24*3600;
	cycleInput[0].cycleData[flag->fertileWindowStartConfirm] = psNext_1 - 19*24*3600;
	cycleInput[0].cycleData[flag->fertileWindowStartForecast] = psNext_1 - 19*24*3600;

	cycleInput[0].cycleData[flag->nextDangerWindowEndForecast] = psNext_1 + (userData->averageCycleLength-11)*24*3600;
	cycleInput[0].cycleData[flag->nextDangerWindowStartForecast] = psNext_1 + (userData->averageMenstruationLength)*24*3600;;
	cycleInput[0].cycleData[flag->nextFertileWindowEndForecast] = psNext_1 + (userData->averageCycleLength-13)*24*3600;
	cycleInput[0].cycleData[flag->nextFertileWindowStartForecast] = psNext_1 + (userData->averageCycleLength-19)*24*3600;
	cycleInput[0].cycleData[flag->nextMenstruationEndForecast] = psNext_1+userData->averageMenstruationLength*24*3600;
	cycleInput[0].cycleData[flag->nextMenstruationStartForecast] = psNext_1;
	cycleInput[0].cycleData[flag->nextOvulationDayForecast] = psNext_1 + (userData->averageCycleLength-14)*24*3600;
	cycleInput[0].cycleData[flag->ovulationDayBBTRise] = 0;
	cycleInput[0].cycleData[flag->ovulationDayConfirm] = psNextReal == 1 ? psNext_1 - 14*24*3600:0;
	cycleInput[0].cycleData[flag->ovulationDayForecast] = psNextReal == 1 ? psNext_1 - 14*24*3600:ps+userData->averageOvulationDay*24*3600;
	cycleInput[0].cycleData[flag->ovulationDayNextMenstruation] = psNextReal == 1 ? psNext_1 - 14*24*3600:0;
	cycleInput[0].cycleData[flag->ovulationDayUserRecord] = 0;
	cycleInput[0].cycleData[flag->peakDay] = 0;
	return 0;
}


